# Copyright 2017-2020 Authors of Cilium
# SPDX-License-Identifier: Apache-2.0

include ../../../Makefile.defs
include ../../../Makefile.quiet

DEFAULT_OUT := connectivity-check.yaml
# Same as connectivity-check.yaml, but internal traffic only (i.e. without external traffic to 1.1.1.1 or www.google.com)
INTERNAL_TRAFFIC_OUT := connectivity-check-internal.yaml
HOSTPORT_OUT := connectivity-check-hostport.yaml
SINGLE_OUT := connectivity-check-single-node.yaml
PROXY_OUT := connectivity-check-proxy.yaml
QUARANTINE_OUT := connectivity-check-quarantine.yaml
TOOLS_OUT := connectivity-debug-tools.yaml

SERVERS_NAME := echo-a echo-b echo-b-host echo-b-host-headless echo-c echo-c-host echo-c-host-headless
SERVERS_OUT := servers.yaml

SRC := $(wildcard *.cue)
ALL_TARGETS := $(DEFAULT_OUT) $(INTERNAL_TRAFFIC_OUT) $(HOSTPORT_OUT) $(SINGLE_OUT) $(PROXY_OUT) $(QUARANTINE_OUT) $(TOOLS_OUT)

DOCKER_RUN := $(CONTAINER_ENGINE) container run --rm \
	--workdir /src/examples/kubernetes/connectivity-check \
	--volume $(CURDIR)/../../..:/src \
	--user "$(shell id -u):$(shell id -g)"

CUE_VERSION := "v0.2.2"
CUE_SHA := "2ea932c771212db140c9996c6fff1d236f3f84ae82add914374cee553b6fc60c"
CUE_IMAGE := "docker.io/cuelang/cue:$(CUE_VERSION)@sha256:$(CUE_SHA)"
CUE := $(DOCKER_RUN) $(CUE_IMAGE)

all: $(ALL_TARGETS)

$(DEFAULT_OUT): $(SRC)
	@echo '# Automatically generated by Makefile. DO NOT EDIT' > $@
	$(QUIET)$(CUE) dump >> $@

$(INTERNAL_TRAFFIC_OUT): $(SRC)
	@echo '# Automatically generated by Makefile. DO NOT EDIT' > $@
	$(QUIET)$(CUE) -t traffic=internal dump >> $@

$(HOSTPORT_OUT): $(SRC)
	@echo '# Automatically generated by Makefile. DO NOT EDIT' > $@
	$(QUIET)$(CUE) -t component=all dump >> $@

$(SINGLE_OUT): $(SRC)
	@echo '# Automatically generated by Makefile. DO NOT EDIT' > $@
	$(QUIET)$(CUE) -t topology=single-node dump >> $@

$(PROXY_OUT): $(SRC) $(SERVERS_OUT)
	@echo '# Automatically generated by Makefile. DO NOT EDIT' > $@
	$(QUIET)$(CUE) -t component=proxy dump >> $@
	@cat $(SERVERS_OUT) >> $@

$(QUARANTINE_OUT): $(SRC) $(SERVERS_OUT)
	@echo '# Automatically generated by Makefile. DO NOT EDIT' > $@
	$(QUIET)$(CUE) -t component=all -t quarantine=true dump >> $@
	@cat $(SERVERS_OUT) >> $@

$(TOOLS_OUT): $(SRC) $(SERVERS_OUT)
	@echo '# Automatically generated by Makefile. DO NOT EDIT' > $@
	$(QUIET)$(CUE) -t component=all -t type=tool dump >> $@

clean:
	@rm -f $(ALL_TARGETS)
	@rm -f *.new
	@rm -f *.diff
	@rm -f *.yaml

deploy:
	$(QUIET)kubectl apply -f connectivity-check-hostport.yaml

eval:
	$(QUIET)$(CUE) eval -c ./...

# To easier inspect individual yamls for specific checks, generate all YAMLs from *.cue
generate_all: $(SRC)
	# TODO: Don't run docker for every command
	@for deployment in $(shell $(CUE) -t component=all ls | tail -n+2 | awk '{ print $$4 }' | sort | uniq); do \
		$(CUE) -t component=all -t name=$$deployment dump > $$deployment.yaml; \
	done

inspect:
	@echo "Comparing latest cue declarations against cluster deployment from "make deploy"..."
	$(QUIET)$(CUE) -t component=all dump > connectivity-check-hostport.yaml.new
	-@kubectl diff -f connectivity-check-hostport.yaml.new > connectivity-check-hostport.yaml.diff
	@cat connectivity-check-hostport.yaml.diff
	@echo
	@echo "The full diff is available in connectivity-check-hostport.yaml.diff."

help:
	$(QUIET)$(CUE) cmd help
	@echo "The cue CLI may be installed via the following command:"
	@echo "$$ go get cuelang.org/go@$(CUE_VERSION)"

list:
	$(QUIET)$(CUE) cmd ls

fmt:
	$(QUIET)$(CUE) fmt

$(SERVERS_OUT): $(SRC)
	@echo > $(SERVERS_OUT)
	@for name in $(SERVERS_NAME); do \
		$(CUE) cmd -t component=all -t name=$$name dump >> $@; \
	done

.PHONY: all clean deploy eval fmt generate_all help inspect list $(ALL_TARGETS)
